using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace pong1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// PongScore is responsible for storing and updating the score.
    /// </summary>
    public class PongScore : Microsoft.Xna.Framework.DrawableGameComponent
    {
        EventCollection eventCollection;    // Stores reference to pending events list
        String[] eventsForThisGameObjectArray;  // Stores a list of events that this object polls for
        String strScoreName;    // Stores name of any texture object if needed
        String strEventReceiver;    // Event receiver name of PongScore object
        SpriteBatch spriteBatch;    // Stores reference to spritebatch retrieved from Services object
        SpriteFont spriteFont;      // Stores reference to spritefont retrieved from Services object
        int score1, score2;
        String scoreText1, scoreText2;

        public PongScore(Game game)
            : base(game)
        {
            strEventReceiver = "score";
            eventCollection = (EventCollection)Game.Services.GetService(typeof(EventCollection));
            eventCollection.RegisterForEvent("updatescore", strEventReceiver);    // register for message from ball
            score1 = score2 = 0;
            scoreText1 = "Player1: " + score1.ToString();
            scoreText2 = "Player2: " + score2.ToString();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// 
        /// Gets the events that it's listening for from the eventsCollection object
        /// </summary>
        public override void Initialize()
        {
            String eventsForThisObject;
            eventsForThisObject = eventCollection.GetEventsForReceiver(strEventReceiver);
            eventsForThisGameObjectArray = eventsForThisObject.Split(',');
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!eventCollection.IsEmpty())
            {
                Event e;
                for (int j = 0; j < eventsForThisGameObjectArray.Length; j++)       // For each event this paddle is listening for, retrieve and process the event
                {
                    e = eventCollection.RetrieveEvent(eventsForThisGameObjectArray[j]); 

                    if (e != null)
                    {
                        processEvent(e);
                    }
                }

            }
            scoreText1 = "Player1: " + score1.ToString();
            scoreText2 = "Player2: " + score2.ToString();
            base.Update(gameTime);
        }

        /// <summary>
        /// The event processor for all events handled by PongBall
        /// 
        /// </summary>
        /// <param name="e"></param>
        private void processEvent(Event e)
        {

            switch (e.EventName)
            {
                case "updatescore":
                    if (e.EventArguments[0].ArgumentValue == "left")
                        score2++;
                    else
                        score1++;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Draws the score
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            spriteFont = (SpriteFont)Game.Services.GetService(typeof(SpriteFont));
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            spriteBatch.Begin();
            spriteBatch.DrawString(spriteFont, scoreText1, new Vector2(GraphicsDevice.Viewport.Width / (1.5f), 10), Color.White);
            spriteBatch.DrawString(spriteFont, scoreText2, new Vector2(GraphicsDevice.Viewport.Width / 4, 10), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
